package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class SpringAmqpListener {

    //helloword模型的监听
//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueueMessage(String msg) {
//        System.out.println("简单队列，监听到消息：" + msg);
//    }

    //work模型的监听
//    @RabbitListener(queues = "simple.queue")
//    public void workqueue(String msg) throws InterruptedException {
//        System.out.println("消费者1，监听到消息：" + msg+"---"+LocalDateTime.now());
//        Thread.sleep(20);
//    }
//    @RabbitListener(queues = "simple.queue")
//    public void workqueue2(String msg) throws InterruptedException {
//        System.err.println("消费者2，监听到消息：" + msg+"---"+LocalDateTime.now());
//        Thread.sleep(100);
//    }

    //fanout模型的监听
//    @RabbitListener(queues = "fanout_queue1")
//    public void fanoutExchange(String msg) throws InterruptedException {
//        System.out.println("fanout消费者1，监听到消息：" + msg+"---"+LocalDateTime.now());
//    }
//
//    @RabbitListener(queues = "fanout_queue2")
//    public void fanoutExchange2(String msg) throws InterruptedException {
//        System.out.println("fanout消费者2，监听到消息：" + msg+"---"+LocalDateTime.now());
//    }

    //direct模型的监听
//    @RabbitListener(bindings = @QueueBinding(
//        exchange = @Exchange(name = "itcast.direct",type = "direct"),
//        value = @Queue(name = "direct.queue1"),
//        key = {"blue", "red"}
//    ))
//    public void directExchange(String msg){
//        System.out.println("direct消费者1，监听到消息：" + msg);
//    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            exchange = @Exchange(name = "itcast.direct",type = "direct"),
//            value = @Queue(name = "direct.queue2"),
//            key = {"yellow", "red"}
//    ))
//    public void directExchange2(String msg){
//        System.out.println("direct消费者2，监听到消息：" + msg);
//    }

    //topic模型的监听
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueue1(String msg){
        System.out.println("消费者接收到topic.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void listenTopicQueue2(String msg){
        System.out.println("消费者接收到topic.queue2的消息：【" + msg + "】");
    }

}
